//
//  ScheduleDayController.m
//  YunDongGym
//
//  Created by lqq on 2018/8/7.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "ScheduleDayController.h"
#import "I_Schedule.h"
#import "SchduleDayCell.h"
#import "MemberListViewController.h"
#import "ScheduleMemberListController.h"
@interface ScheduleDayController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *timeTableView;
@property (nonatomic, strong) UITableView *contentTableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation ScheduleDayController

#pragma mark Class Method
+ (instancetype)controllerWithDate:(NSDate *)date
{
    ScheduleDayController *vc = [[ScheduleDayController alloc] init];
    vc.date = date;
    return vc;
}

#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, 28)];
    self.contentTableView.tableHeaderView = headerView;

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestCoachScheduleInOneDay];
}

#pragma mark Config/UI
- (void)configUI
{
    _timeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 46, 0) style:UITableViewStylePlain];
    _timeTableView.backgroundColor = [UIColor colorWithHexString:@"F4F6F9"];
    _timeTableView.tableFooterView = [UIView new];
    _timeTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _timeTableView.showsVerticalScrollIndicator = NO;
    _timeTableView.insetBottom = 20;
    [self.view addSubview:_timeTableView];
    [_timeTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.view);
        make.top.bottom.equalTo(self.view).offset(-14);
        make.width.equalTo(@(46));
    }];
    _timeTableView.delegate = self;
    _timeTableView.dataSource = self;
    
    
    _contentTableView = [[UITableView alloc] initWithFrame:CGRectMake(46, 0, kDeviceWidth-46, self.view.height) style:UITableViewStylePlain];
    _contentTableView.backgroundColor = [UIColor colorWithHexString:@"F4F6F9"];
    _contentTableView.tableFooterView = [UIView new];
    _contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _contentTableView.insetBottom = 50;
    [self.view addSubview:_contentTableView];
    [_contentTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.view);
        make.top.bottom.equalTo(self.view).offset(-14);
        make.left.equalTo(self.timeTableView.mas_right);
    }];
    _contentTableView.delegate = self;
    _contentTableView.dataSource = self;
    [_contentTableView registerNib:[UINib nibWithNibName:NSStringFromClass([SchduleDayCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SchduleDayCell class])];
    
    if ([_contentTableView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        if (@available(iOS 11.0, *)) {
            _timeTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _contentTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}
- (void)showEmptyView
{
    [self.view showEmptyWithDesc:@"暂无日程安排" callBackTitle:nil callBack:nil];
}

#pragma mark NET
- (void)requestCoachScheduleInOneDay
{
    if (self.dataSource.count == 0) {
        [self.view showLoading];
    }
    WeakSelf(self);
    [I_Schedule requestCoachScheduleInOneDayWithDate:_date success:^(NSMutableArray *list) {
        weakself.dataSource = list;
        [weakself.contentTableView reloadData];
        [weakself.timeTableView reloadData];
        [weakself.view hideStatusView];
        if (weakself.dataSource.count == 0) {
            [weakself showEmptyView];
        }
    } failue:^(NSError *error) {
        if (weakself.dataSource.count == 0) {
            [weakself.view showError:error refreshBlock:^{
                [weakself requestCoachScheduleInOneDay];
            }];
        }
    }];

}

#pragma mark UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    M_Schedule *schdule = _dataSource[indexPath.row];
    if (tableView == _timeTableView) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"timeCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"timeCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UILabel *centerLabel = [cell.contentView addCenterLabelWithText:@"" textColor:HEXColor(@"#9B9B9B") font:RegularFont(11)];
            centerLabel.tag = 100;
            cell.contentView.backgroundColor = TP.controllerBackColor;
        }
        UILabel *centerLabel = [cell.contentView viewWithTag:100];
        centerLabel.text = schdule.timeStr;
        return cell;
    } else {
        SchduleDayCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SchduleDayCell class])];
        
        cell.coachSchdule = schdule;
        
        return cell;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == _contentTableView) {
        M_Schedule *schedule = _dataSource[indexPath.row];
        ScheduleMemberListController *schduleMemberListVC = [ScheduleMemberListController controllWithDateInt:schedule.dateInt timeInt:schedule.timeInt];
        [self.navigationController pushViewController:schduleMemberListVC animated:YES];
        
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 56;
    
}

#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView == _contentTableView) {
        _timeTableView.contentOffsetY = _contentTableView.contentOffsetY;
    } else {
        _contentTableView.contentOffsetY = _timeTableView.contentOffsetY;
    }
}


@end
